package org.example.controller.impl;

import org.example.controller.QuestionController;
import org.example.domain.PageBean;
import org.example.domain.Question;
import org.example.domain.response.ResponseResult;
import org.example.services.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("/questions")
public class QuestionControllerImpl implements QuestionController {

    @Autowired
    private QuestionService questionService;

//    @GetMapping("/test")
//    public void test(){
//        if(true){
//            throw new RuntimeException("test错误");
//        }
//    }

    //新增
    @Override
    @PostMapping()
    @PreAuthorize("hasAuthority('question:save')")
    public ResponseResult saveQuestion(@RequestBody @Validated(Question.Save.class) Question question){
        Question questionRes = questionService.saveQuestion(question);
        if(questionRes != null){
            return ResponseResult.success("问题添加成功",questionRes);
        }
        return ResponseResult.failed("问题添加失败");
    }

    //通过id删除
    @Override
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('question:delete')")
    public ResponseResult deleteQuestion(@PathVariable @NotNull(message = "问题ID不能为null")
                                             @Min(value = 0,message = "问题ID不能小于0") int id){
        if(questionService.deleteQuestion(id)){
            return ResponseResult.success("删除成功",null);
        }
        return ResponseResult.failed("删除失败");
    }
    //修改
    @PutMapping()
    @Override
    @PreAuthorize("hasAuthority('question:update')")
    public ResponseResult updateQuestion(@RequestBody @Validated(Question.Update.class) Question question){
        if(questionService.updateQuestion(question)){
            return ResponseResult.success("修改成功",null);
        }
        return ResponseResult.failed("修改失败");
    }

    //通过id查询
    @Override
    @GetMapping("/{questionId}")
    @PreAuthorize("hasAuthority('question:select')")
    public ResponseResult getQuestionByQuestionId(@PathVariable @NotNull(message = "问题ID不能为null") int questionId){
        Question question = questionService.getQuestionByQuestionId(questionId);
        return ResponseResult.success("查询成功",question);
    }

    //分页查询问题信息
    @Override
    @GetMapping("/3/{pageSize}/{currentPage}")
    public ResponseResult getQuestionByPage(@PathVariable @NotNull(message = "pageSize不能为null") int pageSize,
                                            @PathVariable @NotNull(message = "currentPage不能为null") int currentPage){
        PageBean<Question> questionPage = questionService.getQuestionByPage(pageSize,currentPage);
        return ResponseResult.success("查询成功",questionPage);
    }

    //通过问题类型分页查询问题信息
    @Override
    @GetMapping("/2/{type}/{pageSize}/{currentPage}")
    @PreAuthorize("hasAuthority('question:select')")
    public ResponseResult getQuestionByPageAndType(@PathVariable @NotNull(message = "问题类型不能为null") String type,
                                                   @PathVariable @NotNull(message = "pageSize不能为null") int pageSize,
                                                   @PathVariable @NotNull(message = "currentPage不能为null") int currentPage){
        PageBean<Question> questionPage = questionService.getQuestionByPageAndType(type,pageSize,currentPage);
        return ResponseResult.success("查询成功",questionPage);
    }

    //通过题目分页查询（模糊）
    @Override
    @GetMapping("/1/{title}/{pageSize}/{currentPage}")
    @PreAuthorize("hasAuthority('question:select')")
    public ResponseResult getQuestionByPageAndTitle(@PathVariable @NotNull(message = "题目不能为null") String title,
                                                    @PathVariable @NotNull(message = "pageSize不能为null") int pageSize,
                                                    @PathVariable @NotNull(message = "currentPage不能为null") int currentPage){
        PageBean<Question> questionPage = questionService.getQuestionByPageAndTitle(title,pageSize,currentPage);
        return ResponseResult.success("查询成功",questionPage);
    }

}
